package io.renren.com.qywt.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.com.qywt.dao.PurchaseReturnFeeDao;
import io.renren.com.qywt.dto.AftersaleFinanceCollectDTO;
import io.renren.com.qywt.dto.ContractDTO;
import io.renren.com.qywt.dto.MaxContractDTO;
import io.renren.com.qywt.dto.PurchaseReturnInfoDTO;
import io.renren.com.qywt.entity.AftersalePurchasereturnFeeEntity;
import io.renren.commons.dynamic.datasource.annotation.DataSource;
import io.renren.commons.mybatis.service.impl.BaseServiceImpl;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.enums.DeleteEnum;
import io.renren.commons.tools.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TaskPurchaseReturnFeeService extends BaseServiceImpl<PurchaseReturnFeeDao, PurchaseReturnInfoDTO> {
    @Autowired
    private AftersalePurchasereturnFeeService purchasereturnFeeService;
    @Autowired
    private AftersaleFinanceCollectService aftersaleFinanceCollectService;
    @Autowired
    private TaskContractService contractService;

    @DataSource("slave1")
    @Transactional
    public List<AftersalePurchasereturnFeeEntity> getPurchaseReturnInfoList(UserDetail userDetail,AftersaleFinanceCollectDTO dto){
        Map<String,Object> params=new HashMap<>();
        Date collectDate=new Date();
        if(ObjectUtil.isNull(dto)){
            collectDate=DateUtils.addDateDays(new Date(),-1);
        }else{
            collectDate=dto.getCollectDate();
        }
        String collectDateStr=DateUtils.format(collectDate,DateUtils.DATE_PATTERN);
        params.put("collectDate",collectDateStr);
        List<PurchaseReturnInfoDTO> purchaseReturnInfoList=baseDao.getPurchaseReturnInfoList(params);
        List<AftersalePurchasereturnFeeEntity> aftersalePurchasereturnFeeEntityList=new ArrayList<>();

        /**
         * 临时变量
         */
        double weightFee=0.0;
        double numFee=0.0;
        double cubeFee=0.0;

        for(PurchaseReturnInfoDTO purchaseReturnInfoDTO:purchaseReturnInfoList){
            ContractDTO contractDTO = contractService.getContractBySupplierId(purchaseReturnInfoDTO.getSupplierId());
            if(ObjectUtil.isNotNull(contractDTO)) {
                if(contractDTO.getNeedMax() == 1) {//需要修正值
                    MaxContractDTO maxContractDTO = contractService.getMaxContractBySupplierId(purchaseReturnInfoDTO.getSupplierId());
                    if(ObjectUtil.isNotNull(maxContractDTO)){
                        weightFee=purchaseReturnInfoDTO.getTotalQty()*purchaseReturnInfoDTO.getSkuWeight()*maxContractDTO.getRebackWeight();
                        numFee=purchaseReturnInfoDTO.getTotalQty()*maxContractDTO.getRebackNum();
                        cubeFee=purchaseReturnInfoDTO.getTotalQty()*(purchaseReturnInfoDTO.getSkuCube()/1000000)*maxContractDTO.getRebackCube();
                        double max=Collections.max(Arrays.asList(weightFee,numFee,cubeFee));
                        if(max==weightFee){//按照重量计算
                            purchaseReturnInfoDTO.setRecordType(2);
                        }else if(max==numFee){//按照数量收费
                            purchaseReturnInfoDTO.setRecordType(1);
                        }else if(max==cubeFee){
                            purchaseReturnInfoDTO.setRecordType(3);
                        }
                        purchaseReturnInfoDTO.setMaxCost(max);
                    }
                }
                // 按件计费，包括首件含以下，最低计费；首件以上每件按续件费用计费
                Integer rebackNum = contractDTO.getRebackNum();//退供货费首件数
                double rebackFee=contractDTO.getRebackFee();//退供费首件价格
                double rebackAdd=contractDTO.getRebackAdd();//退供费续件价格
                if (purchaseReturnInfoDTO.getTotalQty() <= rebackNum) {
                    purchaseReturnInfoDTO.setTotalCost(purchaseReturnInfoDTO.getTotalQty()*rebackFee);
                } else {
                    purchaseReturnInfoDTO.setTotalCost(purchaseReturnInfoDTO.getTotalQty()*rebackFee+(purchaseReturnInfoDTO.getTotalQty()-rebackNum)*rebackAdd);
                }
            }
            //退货费明细
            AftersalePurchasereturnFeeEntity entity= new AftersalePurchasereturnFeeEntity();
            entity.setBatchNo(collectDateStr.replaceAll("-",""));
            entity.setPurchasereturnNo(purchaseReturnInfoDTO.getPoreturnKey());
            entity.setPurchasereturnDate(purchaseReturnInfoDTO.getReturnDate());
            entity.setReferenceNo(purchaseReturnInfoDTO.getReferNo());
            entity.setProdSku(purchaseReturnInfoDTO.getSku());
            entity.setProdName(purchaseReturnInfoDTO.getItemname());
            entity.setProdNum(purchaseReturnInfoDTO.getTotalQty());
            entity.setProdWeight(purchaseReturnInfoDTO.getSkuWeight());
            entity.setProdVolume(purchaseReturnInfoDTO.getSkuCube());
            entity.setReturnFee(BigDecimal.valueOf(purchaseReturnInfoDTO.getTotalCost()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
            entity.setStatus(1);
            entity.setDelFlag(DeleteEnum.NO.value());
            entity.setTenantCode(userDetail.getTenantCode());
            entity.setCreator(userDetail.getId());
            aftersalePurchasereturnFeeEntityList.add(entity);
        }
        return aftersalePurchasereturnFeeEntityList;
    }
}
